<?php
namespace app\admin\controller;
use think\Db;
class Ajax extends Base
{   
//相册通用操作++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
    //上传图片
    public function addphoto(){
        if( is_post() ){
            
            $path = com_up_pic('photo', 'input-file', false);
            $data = [
                'path' => $path,
                'initialPreview' => ["<img src='/".$path."' style='width:auto;height:160px;' class='kv-preview-data file-preview-image'>",],
                'initialPreviewConfig' => [
                    [
                        'url'       => url('ajax/removephoto'),
                        'key'       => $path,
                    ]
                ],
            ];
            exit(json_encode($data, JSON_UNESCAPED_UNICODE ));
        }
    }
    
    //删除图片
    public function removephoto(){
        if( is_post() ){
            $file = input('key');
            @unlink ($file);
            com_check();
        }
    }
    //不删除图片，但前端需要json格式
    public function notremovephoto(){
        com_check();
    }

    
//快捷通用操作++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
    //复制一条数据
    public function copy( $t ){
        if( is_post() ){
            $rs = db($t)->find(input('id'));
            $rs['title'] = $rs['title']."-拷贝";
            unset($rs['id']);
            db($t)->insert($rs);
    
            $config = [
                'suc' => [
                    'msg' => '复制成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
    //编辑状态
    public function editStatus( $t ){
        if( is_post() ){
            $data   = [
                'id'        => input('id'),
                'status'    => (int)input('val'),
            ];
            db($t)->update( $data );
            $config = [
                'msg' => '编辑状态成功！',
                'suc' => [
                    'js_action' => 'alert|reload',
                ],
            ];
            com_check( $config );
        }
    }
    //编辑排序
    public function editSort( $t ){
        if( is_post() ){
            $data   = [
                'id'    => input('id'),
                'sort'  => (int)input('val'),
            ];
            db($t)->update( $data );
            $config = [
                'suc' => [
                    'msg' => '编辑排序成功！',
                    'js_action' => 'alert',
                ],
            ];
            com_check($config);
        }
    }
    //删除
    public function delete( $t ){
        if( is_post() ){
            db($t)->delete( input('id') );
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
    //确认操作密码
    public function spassword(){
        if( is_post() ){
            if( com_password(input('val')) != $this->userinfo['s_password'] )
                com_error(['操作密码错误']);
            $config = [
                'suc' => [
                    'msg' => '密码正确！',
                    'js_action'=>"json",
                ],
            ];
            com_check( $config );
        }
    }
}